package com.example.tool.widget.keep.bar.plotter;

import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;

import com.example.tool.util.ValueUtils;
import com.example.tool.widget.keep.bar.Axis;
import com.example.tool.widget.keep.bar.AxisUtils;

import java.util.ArrayList;


/**
 * 类描述：
 * 创建人：lqh
 * 创建时间：2018/2/1
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @version v1.0
 */

public class BarChartPlotter implements Plotter {
    int height;
    private Axis axis;
    private Paint mChartPaint;
    private int upColor, bottomColor;
    private int size;
    //    private ArrayList<Integer> list;
    private ArrayList<Float> list;

    //    public BarChartPlotter(Axis axis, ArrayList<Integer> list) {
//        this.axis = axis;
//        this.list = list;
//        mChartPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
//        mChartPaint.setAntiAlias(true);
//        mChartPaint.setStyle(Paint.Style.FILL);
//    }
    public BarChartPlotter(Axis axis, ArrayList<Float> list) {
        this.axis = axis;
        this.list = list;
        mChartPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mChartPaint.setAntiAlias(true);
        mChartPaint.setStyle(Paint.Style.FILL);
    }


    public void setColor(int upColor, int bottomColor) {
//        mChartPaint.setColor(color);
        this.upColor = upColor;
        this.bottomColor = bottomColor;
        mChartPaint.setColor(bottomColor);
    }

    @Override
    public void draw(Canvas canvas, int height, float current) {
        this.height = height;
        size = height / 15;
        for (int i = 0; i < axis.getShortLineCount(); i++) {
            drawBarChart(canvas, i, (int) current);
        }

    }

    /**
     * 画圆柱形
     *
     * @param canvas   画布
     * @param position 画的位置
     * @param current  移动到当前位置
     */
    private void drawBarChart(Canvas canvas, int position, int current) {
        float mChartWidth = axis.distance * position + axis.getBorderOffset();
        //渐变颜色数据
        int[] colors = new int[]{upColor, bottomColor};

        //画柱状图
        RectF rectF = new RectF();
        rectF.left = mChartWidth - size;
        rectF.right = mChartWidth + size;
        //底部圆角去掉
        rectF.bottom = height + size;
        //柱状图高度最大值  180为最高点
        float heightTopMax = AxisUtils.dpToPixel(ValueUtils.BAR_MEASURE_HEIGHT + 22);
        rectF.top = height - list.get(position) * heightTopMax;

        //圆柱半径圆角
//        int radius = 120;
        int radius = 100;
        if (current != position) {
            //渐变功能
            LinearGradient lg = new LinearGradient(rectF.left, rectF.top, rectF.right,
                    rectF.bottom, colors, new float[]{0.2f, 1.0f}, Shader.TileMode.CLAMP);
            mChartPaint.setShader(lg);
        } else {
            //停在某点的时候高亮显示
            LinearGradient lg = new LinearGradient(rectF.left, rectF.top, rectF.right,
                    rectF.bottom, colors, new float[]{1f, 0f}, Shader.TileMode.CLAMP);
            mChartPaint.setShader(lg);
        }

        if (mChartWidth < axis.currMinPixel - axis.distance || mChartWidth > axis.currMaxPixel + axis.distance) {
            return;
        }
        float barHeight = rectF.bottom - rectF.top;
        //如果bar高度小于半径
        if (barHeight < radius) {
            canvas.drawRoundRect(rectF, radius, barHeight, mChartPaint);
        } else {

            canvas.drawRoundRect(rectF, radius, radius, mChartPaint);
        }
    }

}
